home *** CD-ROM | disk | FTP | other *** search
/ WINMX Assorted Textfiles / Ebooks.tar / Text - Tech - Cryptology - The Math Behind the RSA Cipher.txt < prev    next >
Text File  |  2003-09-23  |  38KB  |  905 lines

  1.            Prime Number Hide-and-Seek: How the RSA Cipher Works
  2.  
  3.      Table of Contents
  4.    * Preface: What is This?
  5.    * Introduction: The Idea of a Trapdoor Function
  6.    * Background, Part I: How to Calculate with Exponents
  7.    * Background, Part II: Modulus Arithmetic
  8.    * Background, Part III: The Fundamental Theorem of Algebra
  9.    * Background, Part IV: Relatively Prime Numbers
  10.    * Euler's Totient Function
  11.    * Euler's Totient Theorem
  12.    * Variations on a Theme
  13.    * The Plot Thickens
  14.    * Does This Really Work?
  15.    * Making a Pair of Keys
  16.    * An Example
  17.    * How to Crack RSA
  18.    * How to Make RSA Uncrackable
  19.    * Huge Exponents in Modulus Arithmetic
  20.    * Safety in Numbers
  21.    * References
  22.  
  23. Preface: What is This?
  24.  
  25. The RSA cipher is a fascinating example of how some of the most abstract
  26. mathematical subjects find applications in the real world. Few are the
  27. mathematicians who study creatures like the prime numbers with the hope or
  28. even desire for their discoveries to be useful outside of their own domain.
  29. But every now and then that is exactly what happens.
  30.  
  31. This text explains the mathematics behind RSA -- how and why it works. The
  32. intended audience is just about anyone who is interested in the topic and
  33. who can remember a few basic facts from algebra: what a variable is, the
  34. difference between a prime number and a composite number, and the like.
  35.  
  36. The most important mathematical facts necessary for understanding RSA's
  37. foundations are reviewed near the beginning. Even if you are familiar with
  38. everything covered in these sections, I would recommend that you at least
  39. skim through them.
  40.  
  41. In one or two places, I have specifically targeted an explanation to what I
  42. consider to be the average computer programmer, leveraging analogous
  43. concepts in programming and general mathematics.
  44.  
  45. Before getting started, I should make some observations on the mathematical
  46. notation used here.
  47.  
  48. For the most part, where notations for the same idea differ between
  49. standard mathematics and the common practices among computer programmers, I
  50. have stuck with the mathematicians. This is, after all, a mathematical
  51. subject. However, I have deviated in a few places where there was too much
  52. opportunity for confusion. I have used * to denote multiplication, and have
  53. completely avoided "implied" multiplication (i.e., using PQ as shorthand
  54. for P * Q). Since not all web browsers can display superscripts, I have
  55. used ^ to denote exponentiation. (This necessitates more parenthesizing
  56. than would normally be used.) The mathematician's three-bar congruency
  57. symbol is not available, so I have made do with = instead. Variables are
  58. always named with a single capital letter.
  59.  
  60. Finally, please note that throughout the text I use the word number to
  61. refer specifically to a positive integer -- what are sometimes referred to
  62. as the natural numbers, or counting numbers.
  63.  
  64.   ------------------------------------------------------------------------
  65.  
  66. Introduction: The Idea of a Trapdoor Function
  67.  
  68. What a mathematician refers to as a function is very similar to a function
  69. in computer programming. It is, in essence, an abbreviation. For example:
  70.  
  71.   F(X) = 7 * X + 43.
  72. If X happens to be 3, then F(X) will be 64. So, "F(3)" is shorthand for
  73. "7 * 3 + 43".
  74.  
  75. The same function in a C program might look like:
  76.  
  77.         int F(int x)
  78.         {
  79.             return 7 * x + 43;
  80.         }
  81.  
  82. Of course, in a computer program, functions are used to encapsulate all
  83. kinds of algorithms, and frequently make use of external variables and the
  84. like. In mathematics, however, a function is used solely for the number it
  85. returns. And, given a certain number as input, they will always return the
  86. same output. (Thus, rand() would not qualify as a mathematical function,
  87. unless it were written so that the seed value was passed in as an input
  88. parameter.)
  89.  
  90. Mathematicians often consider how to construct a function's inverse --
  91. taking a function and making a new one that "goes in the other direction",
  92. so to speak:
  93.  
  94.   G(X) = (X - 43) / 7.
  95. G(64) is equal to 3, and in general, G(F(X)) is equal to X. Therefore, G is
  96. F's inverse. Not all functions are invertible, of course. Clearly, the
  97. function:
  98.  
  99.   F(X) = X * 0
  100. cannot be inverted. (Because how could G(F(X)) return X when F(X) is always
  101. zero?)
  102.  
  103. Usually, when you have a mathematical function for which an inverse does
  104. exist, constructing it is not too difficult. In fact, it is often
  105. transparent. Typically, you can just run through the steps backwards,
  106. subtracting where the original function adds, and so on. But can it be done
  107. for every invertible function?
  108.  
  109. To put the question in terms of programming, imagine that there are two
  110. functions:
  111.  
  112.         int foo(int x);
  113.         int bar(int x);
  114.  
  115. foo() and bar() work like mathematical functions -- they do nothing but
  116. compute a return value, and given the same number for input, they will
  117. always produce the same output. (And pretend for the moment that this is on
  118. a machine where integers can be arbitrarily large.) Suppose you are told
  119. that bar() is the inverse of foo(). The statement:
  120.  
  121.         x == bar(foo(x))
  122.  
  123. is always true, as long as x meets foo()'s requirements for a valid
  124. argument.
  125.  
  126. Now, imagine that you have the source code for foo(), but not for bar().
  127. Can you write your own replacement for bar(), just by examining foo()?
  128.  
  129. It seems that you ought to be able to. There are no secrets as to what
  130. foo() does, after all. You can run foo() with different inputs as many
  131. times as you like. You already know that bar() exists, somewhere, so you
  132. that it is possible to write. Is it guaranteed that you can reconstruct it?
  133.  
  134. Theoretically speaking, the answer is yes. Given such an function, it is
  135. always possible to construct its inverse. However, if we also throw in the
  136. tiny constraint that you have to finish before the heat-death of the
  137. universe, the answer subtly changes.
  138.  
  139. There are some special functions that, though what they do is simple
  140. enough, and how they do what they do is utterly transparent, figuring out
  141. how to undo what they do is a diabolical task. Such a creature is a
  142. trapdoor function. Anyone can fall through a trapdoor, but only those who
  143. know where the hidden lever is can climb back out again.
  144.  
  145. In 1975, Whitfield Diffie, Martin E. Hellman, and Ralph Merkle realized
  146. that a trapdoor function could be the basis for an entirely new kind of
  147. cipher -- one in which the decoding method could remain secret even when
  148. the encoding method was public knowledge. Diffie and Hellman published a
  149. paper in 1976 that described this idea, and offered some examples of weak
  150. trapdoor functions. And in 1977, Ronald L. Rivest, Adi Shamir, and Leonard
  151. Adleman outlined, in an MIT technical memo, an excellent candidate that
  152. became the basis for the RSA cipher.
  153.  
  154. What follows is a description of that function.
  155.  
  156.   ------------------------------------------------------------------------
  157.  
  158. Background, Part I: How to Calculate with Exponents
  159.  
  160. Here's a quick refresher on how to combine exponents. Recall that:
  161.  
  162.   N^2 = N * N,
  163.   N^3 = N * N * N,
  164.   N^4 = N * N * N * N,
  165.  
  166. and so on. For example:
  167.  
  168.   2^7 = 2 * 2 * 2 * 2 * 2 * 2 * 2 = 128.
  169. If we fiddle with exponents for a bit, we will quickly realize that:
  170.  
  171.   N^E * N = N^(E + 1).
  172. So, for example:
  173.  
  174.   2^7 * 2 = 128 * 2 = 256 = 2^8.
  175. Building upon this, we can also see that:
  176.  
  177.   N^E * N * N = N^(E + 2).
  178. But N * N can also be written as N^2:
  179.  
  180.   N^E * N^2 = N^(E + 2).
  181. We can extrapolate from this, and derive a more general rule -- namely:
  182.  
  183.   N^A * N^B = N^(A + B).
  184. And, if we repeated this process on the next level up, we would find that:
  185.  
  186.   (N^A)^B = N^(A * B).
  187. These two simple facts are very useful when handling exponent-laden
  188. formulas.
  189.  
  190. Background, Part II: Modulus Arithmetic
  191.  
  192. Most computer programmers are familiar with modulus as a "remainder"
  193. operator, usually denoted by "%", which gives the remainder of an integer
  194. division instead of the quotient. For example:
  195.  
  196.   27 % 12 = 3.
  197. Though the idea is the same, the mechanics here are slightly different from
  198. what mathematicians refer to as modulus arithmetic. In essence, modulus
  199. arithmetic consists of taking the infinitely long number-line and coiling
  200. it around a finite circle. All the numbers that land on the same point
  201. along the circle's edge are considered interchangeable, or congruent. Thus,
  202. the analogue to the above example in modulus arithmetic would be expressed
  203. as:
  204.  
  205.   27 = 3 (mod 12),
  206. or, in words:
  207.  
  208.   27 is congruent to 3, modulo 12.
  209. (Though note that mathematicians actually use a three-barred version of the
  210. equal sign to indicate congruency.) In this case, 12 is the modulus that we
  211. are working under, and the equation simply tells us that, under a modulus
  212. of 12, 27 and 3 are considered to be the same number. Likewise:
  213.  
  214.   11 + 16 = 3 (mod 12)
  215. reads as:
  216.  
  217.   11 plus 16 is congruent to 3, modulo 12.
  218. Modulus arithmetic is sometimes called clockface arithmetic -- if it's
  219. currently 11 o'clock, then 16 hours later it will be 3 o'clock. (Of course,
  220. the analogy is less perfect when the modulus is something other than 12.)
  221.  
  222. An important feature of modulus arithmetic is that you can replace the
  223. terms of an addition operation with congruent values, and still get the
  224. right answer:
  225.  
  226.   16 = 4 (mod 12), therefore
  227.   11 + 16 = 11 + 4 = 3 (mod 12).
  228. Another example:
  229.  
  230.   9835 = 7 (mod 12), and
  231.   1176 = 0 (mod 12), therefore
  232.   9835 + 1176 = 7 + 0 = 7 (mod 12).
  233. Even better, this trick also works with multiplication:
  234.  
  235.   9835 * 1176 = 7 * 0 = 0 (mod 12)
  236. (and, if we check, we will see that, yes, 9835 * 1176 is 11565960, and
  237. 11565960 = 0 (mod 12)).
  238.  
  239. If our modulus was 10, then modulus arithmetic would be equivalent to
  240. ignoring all but the last digit in our numbers:
  241.  
  242.   37 = 7 (mod 10),
  243.   287 + 482 = 9 (mod 10), and
  244.   895 * 9836 = 0 (mod 10).
  245. And, in a sense, a C program does all of its calculations in modulus
  246. arithmetic. Since integer calculations in C are permitted to overflow, the
  247. high bits silently falling off into the bit bucket, a C program using
  248. 32-bit integers is really doing all of its arithmetic modulo 2^32.
  249.  
  250. As you might imagine, some calculations that are time-consuming and produce
  251. huge numbers become trivial in modulus arithmetic. The ability to reduce
  252. values to their remainders before doing the actual calculation keeps the
  253. calculations from getting out of hand.
  254.  
  255. Background, Part III: The Fundamental Theorem of Algebra
  256.  
  257. The Fundamental Theorem of Algebra states that for every number, there is
  258. exactly one way to factor that number into primes -- and vice versa: every
  259. selection of primes multiplies into a different number. For example:
  260.  
  261.   1176 = 2 * 2 * 2 * 3 * 7 * 7, or
  262.   1176 = 2^3 * 3^1 * 7^2.
  263. It is guaranteed that there is no other way to break 1176 into prime
  264. factors. And, certainly, any time you take three 2s, two 7s, and a three,
  265. you're always going to get 1176 when you multiply them together. The
  266. Fundamental Theorem of Algebra assures us that both these things are true
  267. for every number.
  268.  
  269. (By the way, this is one of the reasons that 1 is not considered to be a
  270. prime number: if it were, then each number would have an infinite number of
  271. prime factorizations, all differing by how many 1s were included. Instead,
  272. 1 is considered to have no prime factors at all, and we say that a number
  273. is prime if it has exactly one prime factor -- namely itself.)
  274.  
  275. Put another way, the Fundamental Theorem of Algebra states that the set of
  276. all numbers and the set of all selections of prime numbers are "isomorphic"
  277. -- there is a perfect one-to-one mapping between the two. A number is
  278. therefore defined by its prime factorization.
  279.  
  280. Background, Part IV: Relatively Prime Numbers
  281.  
  282. The greatest common divisor (abbreviated GCD) of two numbers is the largest
  283. number that evenly divides into both of them. For example:
  284.  
  285.   GCD(15, 10) = 5,
  286.   GCD(18, 10) = 2,
  287.   GCD(21, 10) = 1, and
  288.   GCD(170, 102) = 34.
  289. Or, another way to look at it is to say that the GCD is the intersection of
  290. the two numbers' set of prime factors:
  291.  
  292.   GCD((2^3 * 3^1 * 7^2), (2^2 * 5^1 * 7^3)) = 2^2 * 7^2, so
  293.   GCD(1176, 6860) = 196.
  294. When two numbers have no common factors, their GCD will be 1, and the two
  295. numbers are said to be relatively prime (or coprime). For example, we can
  296. see in our list up above that 21 and 10 are relatively prime.
  297.  
  298. Since a prime number has no factors besides itself, clearly a prime number
  299. is relatively prime to every other number. And the same can be said of the
  300. number 1.
  301.  
  302. Okay. Enough background material. Let's get to the good stuff.
  303.  
  304.   ------------------------------------------------------------------------
  305.  
  306. Euler's Totient Function
  307.  
  308. Euler's Totient Function is denoted by the Greek letter phi, and is defined
  309. as follows:
  310.  
  311.      phi(N) = how many numbers between 1 and N - 1 which are
  312.      relatively prime to N.
  313.  
  314. Thus:
  315.  
  316.   phi(4) = 2    (1 and 3 are relatively prime to 4),
  317.   phi(5) = 4    (1, 2, 3, and 4 are relatively prime to 5),
  318.   phi(6) = 2    (1 and 5 are relatively prime to 6),
  319.   phi(7) = 6    (1, 2, 3, 4, 5, and 6 are relatively prime to 7),
  320.   phi(8) = 4    (1, 3, 5, and 7 are relatively prime to 8), and
  321.   phi(9) = 6    (1, 2, 4, 5, 7, and 8 are relatively prime to 9).
  322.  
  323. Here is the same definition expressed as C code:
  324.  
  325.         phi = 1;
  326.         for (i = 2 ; i < N ; ++i)
  327.             if (gcd(i, N) == 1)
  328.                 ++phi;
  329.  
  330. (By the way, notice that phi(1) is specially defined to be 1.)
  331.  
  332. It should be easy to see that phi(N) will be N - 1 whenever N is prime.
  333. Somewhat less obvious is the useful fact that phi(N) is also easy to
  334. calculate when N has exactly two different prime factors:
  335.  
  336.   phi(P * Q) = (P - 1) * (Q - 1), if P and Q are prime.
  337. (The proof of this fact is left as an exercise for the reader.) Thus, for
  338. example:
  339.  
  340.   phi(15) = 2 * 4 = 8    (1, 2, 4, 7, 8, 11, 13, and 14).
  341.  
  342. The two prime factors cannot be the same number for this to work, and in
  343. fact you can see above that phi(9) does not equal 4.
  344.  
  345. Euler's Totient Theorem
  346.  
  347. This theorem is one of the important keys to the RSA algorithm:
  348.  
  349.      If GCD(T, R) = 1 and T < R, then T^(phi(R)) = 1 (mod R).
  350.  
  351. Or, in words:
  352.  
  353.      If T and R are relatively prime, with T being the smaller number,
  354.      then when we multiply T with itself phi(R) times and divide the
  355.      result by R, the remainder will always be 1.
  356.  
  357. We can test this theorem on some smaller numbers for which we have already
  358. calculated the totient. Using 5 for T and 6 for R, we get:
  359.  
  360.   phi(6) = (2 - 1) * (3 - 1) = 1 * 2 = 2, so
  361.   5^(phi(6)) = 5^2 = 25, and
  362.   25 = 24 + 1 = 6 * 4 + 1, therefore
  363.   25 = 1 (mod 6).
  364. Using 2 for T and 15 for R, we have:
  365.  
  366.   phi(15) = (3 - 1) * (5 - 1) = 2 * 4 = 8, so
  367.   2^(phi(15)) = 2^8 = 256, and
  368.   256 = 255 + 1 = 17 * 15 + 1, therefore
  369.   256 = 1 (mod 15).
  370.  
  371. Variations on a Theme
  372.  
  373. Here again is the equation of Euler's Totient Theorem:
  374.  
  375.   T^(phi(R)) = 1 (mod R)
  376. (remembering that T < R, and T and R are relatively prime). Thanks to the
  377. way that modulus arithmetic works on multiplication, we can easily see
  378. that:
  379.  
  380.   T^(phi(R)) * T^(phi(R)) = 1 * 1 (mod R),
  381. which can be rewritten, using the laws of exponents, as:
  382.  
  383.   T^(phi(R) + phi(R)) = 1 * 1 (mod R),
  384. or:
  385.  
  386.   T^(2 * phi(R)) = 1 (mod R).
  387. If we ran through this sequence again, we would get:
  388.  
  389.   T^(3 * phi(R)) = 1 (mod R).
  390. Clearly, we could keep doing this as many times as we like. So, we can
  391. expand on Euler's Totient Theorem, and state a more general corollary:
  392.  
  393.      If GCD(T, R) = 1 and T < R, then T^(S * phi(R)) = 1 (mod R),
  394.      where S can be any number.
  395.  
  396. That's our first variation. Now, let's tweak this further by multiplying
  397. both sides by T:
  398.  
  399.   T^(S * phi(R)) * T = 1 * T (mod R).
  400. Simplifying leaves us with:
  401.  
  402.   T^(S * phi(R) + 1) = T (mod R).
  403. If we repeat this, we will get:
  404.  
  405.   T^(S * phi(R) + 1) * T = T * T (mod R),
  406. or:
  407.  
  408.   T^(S * phi(R) + 2) = T^2 (mod R).
  409. Doing this yet again will give us:
  410.  
  411.   T^(S * phi(R) + 3) = T^3 (mod R),
  412. and so on.
  413.  
  414. What makes this so interesting is that S can be any number. It means that,
  415. when calculating the value of:
  416.  
  417.   T^E (mod R),
  418. if E happens to be greater than phi(R), we can subtract phi(R) from E, and
  419. keep on subtracting it until we have a number less than phi(R), and the new
  420. formula will still produce the same value.
  421.  
  422. Guess what? This is just another way of describing modulus arithmetic. So,
  423. what this boils down to is the rather surprising rule:
  424.  
  425.   T^E = T^F (mod R) whenever E = F (mod phi(R)).
  426. (again, as long as T < R, and T and R are relatively prime). In other
  427. words, when doing modulus arithmetic, exponentiation works differently than
  428. addition and multiplication. You can reduce an exponent, but you need to
  429. use phi(R), and not R itself.
  430.  
  431. The Plot Thickens
  432.  
  433. We just blew past something very important. Let's back up and look at this
  434. equation more closely:
  435.  
  436.   T^(S * phi(R) + 1) = T (mod R).
  437. Notice what we have here. We take a number, T, and raise it to a power, and
  438. when we do the calculation in modulus arithmetic, we wind up with T again.
  439. In short, we have a recipe for a function that returns its own input
  440. (presuming that R has been chosen ahead of time, and T is relatively prime
  441. to R).
  442.  
  443. If you're thinking to yourself, "What's so interesting about that?", then
  444. consider what we would have if we broke this function up into two separate
  445. steps. Specifically, let's imagine that we can find two new numbers P and Q
  446. such that:
  447.  
  448.   P * Q = S * phi(R) + 1, for some value of S that we choose.
  449. Then we could rewrite:
  450.  
  451.   T^(S * phi(R) + 1) = T (mod R)
  452. as:
  453.  
  454.   T^(P * Q) = T (mod R).
  455. Now, this is equivalent to:
  456.  
  457.   (T^P)^Q = T (mod R),
  458. and this is something that can be broken up into two steps:
  459.  
  460.   T^P = X (mod R), and then
  461.   X^Q = T (mod R).
  462. Now, if you don't see the value in doing this, imagine now that the two
  463. steps are performed on separate computers. And that X is sent from the
  464. first computer to the second over an insecure phone line....
  465.  
  466. Does This Really Work?
  467.  
  468. T stands for the plaintext, the message that is to be sent. P, Q, and R
  469. together form the cipher's keys -- P and R make up the public key, and Q
  470. and R make up the private key. And X becomes the encrypted message.
  471.  
  472. Here, again, is the central equation that makes it all possible:
  473.  
  474.   P * Q = S * phi(R) + 1.
  475. Note here that P and Q will both automatically be relatively prime to
  476. phi(R). (Why? Because their product, P * Q, is one more than a multiple of
  477. phi(R). Any factors of P or Q must also be factors of P * Q. Any number
  478. that is a factor of S * phi(R) + 1 can't also be a factor of phi(R).) This
  479. is important, since it helps to assure us that a P and Q can actually be
  480. found.
  481.  
  482. Imagine a clockface, with just an hour hand, and imagine yourself placing
  483. the hour hand on midnight and then moving it forward by jumps, over and
  484. over, each jump covering N hours. If you pick a value for N that is
  485. divisible by 2 or 3 (the prime factors of 12), then you will find that you
  486. will only hit certain numbers before you return to midnight, and the
  487. sequence will then repeat. If N is 2, then the hour hand will visit 12, 2,
  488. 4, 6, 8, 10, 12, 2, 4, 6, 8, 10, 12 ...
  489.  
  490. If, however, your N is relatively prime with 12, then you will wind up
  491. hitting every number exactly once before finally returning to midnight 12
  492. jumps later. For example, using 7 for your N, the itinerary would be: 12,
  493. 7, 2, 9, 4, 11, 6, 1, 8, 3, 10, 5, 12, ... In addition, the order in which
  494. you visit the numbers is entirely dependent on what value you pick for N.
  495.  
  496. In a similar vein, it is important that both P and Q be relatively prime to
  497. phi(R). Because of this, we know that every possible value for T, when
  498. raised to the power P modulo R, will land on a different number. (Remember
  499. that when doing exponents in modulus arithmetic, it is actually phi(R), and
  500. not R itself, that determines the length of the cycles.) If this weren't
  501. true -- if P, for example, shared a factor in common with phi(R) -- then
  502. some values for T could get mapped to the same value for X, and it would
  503. clearly be impossible to tell which was originally which. There could not
  504. be one value for Q that would correctly map X back to T every time.
  505.  
  506. The question of which T-values will wind up going to which X-values depends
  507. entirely on the value used for P -- and here's the rub for the would-be
  508. codebreaker: Just about every possible mapping of T-values to X-values does
  509. in fact exist. Somewhere out there is a P that will make that mapping.
  510.  
  511. If this:
  512.  
  513.   T^P = X
  514.   X^Q = T
  515. was the cipher's scheme, there'd be no cipher. With P already being public
  516. knowledge, it's tedious but no great trick to take an X and compute
  517. backwards to T. But, instead, we have this:
  518.  
  519.   T^P = X (mod R)
  520.   X^Q = T (mod R)
  521. as the cipher's scheme, and that changes everything. The modulus arithmetic
  522. erases too much information. There's no way to deduce how many times the
  523. hour hand needs to spin around the clockface when it goes from X back to T.
  524. Without knowing what Q is, a given X could wind up going to any of the
  525. possible values for T.
  526.  
  527. But what is really maddening to our would-be codebreaker is that even when
  528. T and P and X are all known, Q still can't be deduced! (Of course, it
  529. actually can -- but not necessarily within anyone's lifetime. But we're
  530. getting ahead of ourselves.)
  531.  
  532. So, let's see how to make this recipe work.
  533.  
  534.   ------------------------------------------------------------------------
  535.  
  536. Making a Pair of Keys
  537.  
  538. To construct our own personal cipher keys, we need an appropriate value for
  539. R. So, we start by randomly picking two prime numbers, U and V, and
  540. multiplying them together:
  541.  
  542.   R = U * V.
  543. There are two good reasons for selecting a value for R that has exactly two
  544. prime factors. Not only do we have an easy formula for calculating phi(R):
  545.  
  546.   phi(R) = (U - 1) * (V - 1),
  547. but we also want R to be hard to factor. The fewer factors a number has,
  548. the longer it takes to find them.
  549.  
  550. We then need to figure out values for P, Q, and S, such that:
  551.  
  552.   P * Q = S * phi(R) + 1.
  553. When the numbers have been chosen, P and R together become the public key,
  554. and Q and R make up the private key. U, V, and S are no longer needed, and
  555. can be forgotten.
  556.  
  557. An Example
  558.  
  559. In order to see all this in action, we want to stick with numbers that we
  560. can actually work with. So, for our example, we will select the primes 5
  561. and 7 to be our U and V. This gives R a value of 35, and:
  562.  
  563.   phi(35) = (5 - 1) * (7 - 1) = 4 * 6 = 24.
  564. Now, we need to find numbers to fit the equation:
  565.  
  566.   P * Q = S * 24 + 1.
  567. We'd prefer P and Q to not share any factors with each other, since that
  568. would make it easier to derive one from the other. (And we certainly don't
  569. want P and Q to be the same number, since that would turn our trapdoor
  570. cipher into a garden-variety symmetric cipher!) So, P and Q should be
  571. relatively prime. We will try assigning values to S and see what S * 24 + 1
  572. equals. If we can divide the resulting number into two relatively prime
  573. values, then we have a worthy pair.
  574.  
  575.   2 * 24 + 1 =  49 = 7 * 7      (no, factors must be different)
  576.   3 * 24 + 1 =  73 = 73         (we need two numbers)
  577.   4 * 24 + 1 =  97 = 97         (ditto)
  578.   5 * 24 + 1 = 121 = 11 * 11    (nope)
  579.   6 * 24 + 1 = 145 = 5 * 29     (jackpot)
  580.  
  581. We could continue searching -- nothing requires us to take the first value
  582. that works -- but this is fine for our purposes. So, we have 5 for P, our
  583. public key, and 29 for Q, our private key.
  584.  
  585. To make our cipher work, you may recall that the values we use for T must
  586. be less than R, and also relatively prime to R. We also don't want to use 1
  587. for T, because 1 raised to any power whatsoever is going to remain 1. That
  588. leaves us with 23 values we can use for T. We'll use the following
  589. character set:
  590.  
  591.    2  3  4  6 8  9 11 1213 16 17 1819 22 23 2426 27 29 3132 33 34
  592.    A  B  D  E F  G  H  I J  K  L  M N  O  P  R S  T  U  V W  Y  Z
  593.  
  594. (We're missing a few letters, but this can be worked around with some
  595. creative misspellings: KS for X, KW for Q, and K or S for C. In any case,
  596. it's not important for this example.)
  597.  
  598. The message we will encrypt is "VENIO" (Latin for "I come"):
  599.  
  600.    V  E  N  I O
  601.   31  6 19 1222
  602.  
  603. To encode it, we simply need to raise each number to the power of P modulo
  604. R.
  605.  
  606.   V: 31^5 (mod 35) = 28629151 (mod 35) = 26
  607.   E:  6^5 (mod 35) =     7776 (mod 35) =  6
  608.   N: 19^5 (mod 35) =  2476099 (mod 35) = 24
  609.   I: 12^5 (mod 35) =   248832 (mod 35) = 17
  610.   O: 22^5 (mod 35) =  5153632 (mod 35) = 22
  611.  
  612. So, our encrypted message is 26, 6, 24, 17, 22 -- or "SERLO" in our
  613. personalized character set.
  614.  
  615. When the message "SERLO" arrives on the other end of our insecure phone
  616. line, we can decrypt it simply by repeating the process -- this time using
  617. Q, our private key, in place of P.
  618.  
  619.   S: 26^29 (mod 35) = 10819995774...2921981566976 (mod 35) = 31
  620.   E:  6^29 (mod 35) =     36845653286788892983296 (mod 35) =  6
  621.   R: 24^29 (mod 35) = 10620036506...3621199028224 (mod 35) = 19
  622.   L: 17^29 (mod 35) = 48196857210...1825223071697 (mod 35) = 12
  623.   O: 22^29 (mod 35) = 85164331908...9721106030592 (mod 35) = 22
  624.  
  625. The result is 31, 6, 19, 12, 22 -- or "VENIO", our original message.
  626.  
  627. How to Crack RSA
  628.  
  629. Now, let's switch hats. Imagine that we've just managed to pluck the
  630. message "SERLO" off of our wiretap. By looking up the message's destination
  631. in the public-key directory, we find that our message was encrypted with a
  632. value of 35 for R and 5 for P. How do we go about decrypting it when we
  633. don't know the value for Q?
  634.  
  635. Well, we know that that:
  636.  
  637.   P * Q = S * phi(R) + 1.
  638. We can divide both sides of the equation by P, which gives us a formula for
  639. Q:
  640.  
  641.   Q = (S * phi(R) + 1) / P.
  642. S is also unknown, though, so we will have to try plugging in different
  643. numbers for S, and look for values for Q that meet all the necessary
  644. constraints.
  645.  
  646.   (1 * 24 + 1) / 5 =  25 / 5 =  5       (no, same number as P)
  647.   (2 * 24 + 1) / 5 =  49 / 5            (doesn't divide evenly)
  648.   (3 * 24 + 1) / 5 =  73 / 5            (ditto)
  649.   (4 * 24 + 1) / 5 =  97 / 5            (ditto)
  650.   (5 * 24 + 1) / 5 = 121 / 5            (ditto)
  651.   (6 * 24 + 1) / 5 = 135 / 5 = 29       (this could be it!)
  652.  
  653. Each time we find a candidate for Q, we could test it out on a few
  654. messages, and see if it works. (Note that, for example, when S = 11, Q will
  655. have a value of 53, which also meets all the constraints, but does not
  656. decrypt correctly with P = 5.) If 29 hadn't worked and we needed to
  657. continue the search, it would be pretty obvious that we only needed to test
  658. every fifth number, since those are the only numbers which will give us a
  659. result that is evenly divisible by 5. So, even though this process involves
  660. a brute-force search, it is very simple and very fast.
  661.  
  662. So what's the catch? Well, in order to do any of this, we first need to
  663. know the value of phi(R). Of course, we already know that R has exactly two
  664. prime factors, so calculating phi(R) is a snap once we know what those
  665. factors are.
  666.  
  667. Famous last words.
  668.  
  669.   ------------------------------------------------------------------------
  670.  
  671. How to Make RSA Uncrackable
  672.  
  673. Of course, in our case the factors of R can be found by consulting a times
  674. table. So it's not much of a challenge. (For that matter, since we're
  675. encrypting one character at a time, our coded messages would also be
  676. vulnerable to good old-fashioned cryptanalysis).
  677.  
  678. To make it less easy to find R's factors, we need to pick larger prime
  679. numbers for U and V to begin with. If, instead of 5 and 7, we had chosen
  680. 673 and 24971, we would have a value of 16805483 for R, and phi(R) would be
  681. 16779840. (This would also give us enough room to encrypt three bytes at a
  682. time, which pretty much ruins any hope of cryptanalysis.) Looking for a P
  683. and Q pair is no longer something you want to do with pencil and paper, of
  684. course, but it took me less than three minutes to find the usable pair 397
  685. and 211333 -- including the time it took to write and debug a Perl script.
  686.  
  687. On the other hand, it also took me less than three seconds to run "factor"
  688. on 16805483 to obtain 673 and 24971. Armed with those numbers, it wouldn't
  689. take long to derive 211333 from 397. So even these numbers aren't close to
  690. being large enough. We need really big numbers.
  691.  
  692. Well, we can certainly find huge values for R that are difficult to factor.
  693. But how far can we go before it becomes too difficult for us to use the
  694. numbers in the first place?
  695.  
  696. Huge Exponents in Modulus Arithmetic
  697.  
  698. The problem is this: The bigger R gets, the bigger P and Q will be, and P
  699. and Q are to be used as exponents! Even the relatively tame-looking:
  700.  
  701.   9^(9^9)
  702. produces a number over 350 million decimal digits long. How are we going to
  703. be able to encrypt anything without needing terabytes of storage?
  704.  
  705. The trick is that we only need to calculate these exponential values modulo
  706. R. As always, modulus arithmetic simplifies things a great deal.
  707.  
  708. Let's revisit our example, and look at how we could decrypt the first
  709. number, 31, remembering that R = 35 and Q = 29:
  710.  
  711.   31^29 (mod 35) = ?
  712. To start with, we look at Q's binary representation. 29 in binary is 11101,
  713. which means that:
  714.  
  715.   29 = 16 + 8 + 4 + 1, or
  716.   29 = 2^4 + 2^3 + 2^2 + 2^0.
  717. We can now break the exponential calculation apart into several smaller
  718. ones:
  719.  
  720.   31^29 = 31^(2^4 + 2^3 + 2^2 + 2^0)
  721.         = 31^(2^4) * 31^(2^3) * 31^(2^2) * 31^(2^0)
  722.         = 31^(2 * 2 * 2 * 2) * 31^(2 * 2 * 2) * 31^(2 * 2) * 31
  723.         = (((31^2)^2)^2)^2 * ((31^2)^2)^2 * (31^2)^2 * 31.
  724.  
  725. This may look like anything but an improvement, at first. But on a closer
  726. examination, you'll see that we actually have many repeated subterms. This
  727. simplifies matters, particularly when we take advantage of the fact that we
  728. are calculating in modulo 35.
  729.  
  730. We compute the first square in modulus arithmetic:
  731.  
  732.   31^2 = 961 = 16 (mod 35).
  733. By substituting this value into our equation:
  734.  
  735.   31^29 = (((31^2)^2)^2)^2 * ((31^2)^2)^2 * (31^2)^2 * 31 (mod 35),
  736. we get:
  737.  
  738.   31^29 = ((16^2)^2)^2 * (16^2)^2 * 16^2 * 31 (mod 35).
  739. Now by computing that square:
  740.  
  741.   16^2 = 256 = 11 (mod 35),
  742. we will have:
  743.  
  744.   31^29 = (11^2)^2 * 11^2 * 11 * 31 (mod 35).
  745. We keep simplifying in the same fashion:
  746.  
  747.   11^2 = 121 = 16 (mod 35), and
  748.   16^2 = 256 = 11 (mod 35),
  749. and so:
  750.  
  751.   31^29 = 16^2 * 16 * 11 * 31 (mod 35)
  752.         = 11 * 16 * 11 * 31 (mod 35).
  753.  
  754. We can continue to take advantage of the modulus when we do the final
  755. multiplications:
  756.  
  757.   31^29 = 11 * 16 * 11 * 31 (mod 35)
  758.         = 11 * 16 * 341 (mod 35)
  759.         = 11 * 16 * 26 (mod 35)
  760.         = 11 * 416 (mod 35)
  761.         = 11 * 31 (mod 35)
  762.         = 341 (mod 35)
  763.         = 26 (mod 35).
  764.  
  765. Lo and behold: 26, the same result as when we did it the hard way.
  766.  
  767. This binary technique is really no different than how computers normally
  768. compute integer powers. However, the fact that we can break the process
  769. down to successive multiplications allows us to apply the modulus at every
  770. step of the way. This assures us that at no point will our algorithm have
  771. to handle a number larger than (R - 1)^2.
  772.  
  773. Safety in Numbers
  774.  
  775. Okay. So we know that the process of encryption and decryption is still
  776. practical, even if R is immense. But all of this is moot unless we can
  777. still generate the keys. If we want R to be so big that it can't be
  778. factored easily, how are we going to find it in the first place?
  779.  
  780. Well, it turns out that there is an interesting little asymmetry here.
  781. There happen to be relatively cheap ways to test a number for primality.
  782. One of the most famous is based on what is called Fermat's Little Theorem.
  783. Here is the version of this Theorem that we're interested in:
  784.  
  785.      If P is prime, then N^(P - 1) = 1 (mod P) is true for every
  786.      number N < P.
  787.  
  788. (If this seems suspiciously reminiscent of Euler's Totient Theorem, it
  789. should. Euler was the first person to publish a proof of this Theorem, and
  790. his Totient Theorem is a generalization of Fermat's. You can see this for
  791. yourself by remembering that phi(P) = P - 1 when P is prime.)
  792.  
  793. Of course, from a mathematician's point of view, this theorem is mainly
  794. useful for proving that a given number is composite. For example, it just
  795. so happens that:
  796.  
  797.   4^14 (mod 15) = 268435456 (mod 15) = 1,
  798. even though 15 is no prime. Nonetheless, it is also true that:
  799.  
  800.   3^14 (mod 15) = 4782969 (mod 15) = 9, and
  801.   5^14 (mod 15) = 6103515625 (mod 15) = 10.
  802. On the other hand, 17, which is prime, results in 1 every time:
  803.  
  804.   3^16 (mod 17) = 43046721 (mod 17) = 1,
  805.   4^16 (mod 17) = 4294967296 (mod 17) = 1,
  806.   5^16 (mod 17) = 152587890625 (mod 17) = 1, and so on.
  807. So, if we want to know if a number is prime, we can run it through this
  808. test, using (say) 2 as the base. If anything besides 1 results, we know
  809. with certainty that the number is composite. If the answer is 1, however,
  810. we try the test again with 3, and then 4, and so on. If we keep getting
  811. back 1 as the result, it soon becomes astronomically unlikely that the
  812. number is anything but prime. This doesn't constitute proof, mathematically
  813. speaking, but it certainly works for our purposes.
  814.  
  815. There are other tests for primality, some of which are faster. But they all
  816. have at least one thing in common with this test: When they reject a
  817. number, it tells us only that the number can be factored. The test results
  818. give us no information at all as to what the factors might be. How
  819. unfortunate!
  820.  
  821. Unfortunate for the mathematicians, that is. Very fortunate for us.
  822.  
  823. The basic truth is that, in order to find the factors of a composite
  824. number, we're pretty much stuck with using brute force: Divide the number
  825. by all the primes you can get your hands on until one of them goes in
  826. evenly. There are ways to improve on this approach (the Number Field Sieve
  827. currently being the best), but they are complicated, and all they do is
  828. allow you to narrow the scope of the search. They don't reduce the search
  829. enough to make this problem tractable in general.
  830.  
  831. Nor is it likely that new approaches will, either! The real issue is that
  832. the encrypting and decrypting algorithms have a running time that is linear
  833. with respect to the length of R. That is to say, doubling the number of
  834. digits in R doubles the amount of time (roughly) needed to encrypt,
  835. decrypt, and to select the two primes to make a key with. But the
  836. algorithms for factoring R have a running time that is exponential with
  837. respect to the length of R. That is to say, the time (roughly) doubles with
  838. every few digits! (Because every digit added to R makes it ten times
  839. larger, and thus multiplies the number of potential candidates for its
  840. measly two factors.)
  841.  
  842. So if a new technique is suddenly found that makes it a trillion times
  843. faster to factor a number, all we have to do is increase the size of R we
  844. use by enough digits, and the situation will be right back where it started
  845. -- and all it means to us is that it takes a little bit longer to send and
  846. receive our messages. Already some people are using keys that, in order to
  847. factor with the Number Field Sieve, would require more energy than exists
  848. in the known universe.
  849.  
  850. An illustration: To the best of my knowledge, the number used as the
  851. modulus for the RSA-140 challenge is the largest general number than has
  852. been independently factored. (By general, I'm excluding creatures like
  853. Mersenne numbers and Fermat numbers, which have specialized factoring
  854. techniques that are inapplicable elsewhere.) It was completed on February
  855. 2, 1999. Now, the record previous to this was the RSA-130 number, and the
  856. process of factoring it was estimated as taking a total of 1000 MIPS-years
  857. of computer time. RSA-140, a number only 10 decimal digits longer, required
  858. twice that amount.
  859.  
  860. This, finally, is the heart of what makes RSA a trapdoor function: the gap
  861. between obtaining a number with two prime factors, and rediscovering the
  862. factors from the number itself. And the gap just keeps expanding as the
  863. numbers get larger.
  864.  
  865. The breakthrough that would completely destroy RSA's security would be an
  866. algorithm that actually produced a number's factors directly, instead of
  867. merely narrowing the search's scope. Such a thing has not been proven
  868. impossible, and it would probably be very hard to do so. But considering
  869. the renewed attention that has been focused on this problem in the last two
  870. decades, the likelihood of the existence of such an algorithm is once again
  871. starting to appear quite remote. Discovering one would change the face of
  872. number theory as much as RSA has changed the face of cryptography.
  873.  
  874. However -- if this were to happen, there are other trapdoor functions out
  875. there, waiting to be found. Whatever the future of RSA may be, the trapdoor
  876. cipher has certainly changed the face of cryptography forever.
  877.  
  878.   ------------------------------------------------------------------------
  879.  
  880. References
  881.  
  882. 1. Clawson, Calvin C.: "Mathematical Mysteries", 1996, Plenum Press.
  883. (Clawson devotes an entire chapter to the mathematics behind RSA, and it is
  884. this that gave me the inspiration to create this text.)
  885.  
  886. 2. Gardner, Martin: "Penrose Tiles to Trapdoor Ciphers", 1989, W.H. Freeman
  887. & Co. (This is another anthology of Gardner's wonderful columns for
  888. "Scientific American", and includes the column which was the first widely
  889. published description of the RSA cipher -- the one which set the NSA to
  890. frantically running around in circles.)
  891.  
  892. 3. Ribenboim, Paulo: "The Little Book of Big Primes", 1991,
  893. Springer-Verlag. (The title should actually be "The Little Book of Big
  894. Number Theory" -- the book is chock full of theorems and conjectures that
  895. relate to prime numbers.)
  896.  
  897. 4. Wells, David: "The Penguin Dictionary of Curious and Interesting
  898. Numbers", 1986, Penguin Books. (I had to pull this out at the last minute
  899. to find out how many digits were in 9^(9^9). For the curious whose
  900. libraries lack this little gem, the exact number of digits is 369693100.)
  901.  
  902. Texts
  903. Brian Raiter
  904. Muppetlabs
  905.